package com.easy.project.service.system.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.project.base.exception.ServiceException;
import com.easy.project.base.result.SystemErrorCodeEnum;
import com.easy.project.constant.StatusEnum;
import com.easy.project.convert.RoleDomainConvert;
import com.easy.project.domain.dto.system.RoleDTO;
import com.easy.project.domain.dto.system.RoleEditDTO;
import com.easy.project.domain.po.system.SysRolePO;
import com.easy.project.domain.vo.system.SysRoleVO;
import com.easy.project.mapper.system.SysRoleMapper;
import com.easy.project.service.system.ISysRoleService;
import com.easy.project.util.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.easy.project.base.result.SystemErrorCodeEnum.DATA_FIND_ERROR;

/**
 * <p>
 * 系统角色表 服务实现类
 * </p>
 *
 * @author wxz
 * @since 2024-01-10
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRolePO> implements ISysRoleService {

    @Resource
    private RoleDomainConvert roleDomainConvert;

    @Override
    public IPage<SysRoleVO> rolePage(RoleDTO roleDTO) {
        Page<SysRolePO> page = new PageUtils<SysRolePO, RoleDTO>().buildPage(roleDTO);
        LambdaQueryWrapper<SysRolePO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNoneBlank(roleDTO.getRoleName()),SysRolePO::getRoleName,roleDTO.getRoleName());
        lambdaQueryWrapper.like(StringUtils.isNoneBlank(roleDTO.getRoleCode()),SysRolePO::getRoleCode,roleDTO.getRoleCode());
        lambdaQueryWrapper.eq(Objects.nonNull(roleDTO.getStatus()),SysRolePO::getStatus,roleDTO.getStatus());
        page = this.page(page, lambdaQueryWrapper);
        return roleDomainConvert.poPageToVoPage(page);
    }

    @Override
    public List<SysRolePO> getRoleByUserId(Long userId) {
        return baseMapper.getRoleByUserId(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void roleEdit(RoleEditDTO roleEditDTO) {
        Long id = roleEditDTO.getId();
        // 准备基础数据
        SysRolePO sysRolePO;
        if (Objects.isNull(id)){
            // 新增操作
            sysRolePO = roleDomainConvert.editDtoToPo(roleEditDTO);
        }else {
            // 编辑操作
            sysRolePO = getById(id);
            roleDomainConvert.editDtoToPo(roleEditDTO,sysRolePO);
        }
        // 验证数据 验证角色名,角色编码是否重复 角色是否可用
        validate(sysRolePO);
        // 存储数据
        saveOrUpdate(sysRolePO);
    }

    // 验证数据
    private void validate(SysRolePO sysRolePO){
        // 角色未找到
        if (Objects.isNull(sysRolePO)){
            throw new ServiceException(DATA_FIND_ERROR);
        }
        // 判断角色名或者角色编码是否重复
        List<SysRolePO> roleList = baseMapper.getRoleListByNameOrCode(sysRolePO);
        if (CollectionUtil.isNotEmpty(roleList)){
            throw new ServiceException(SystemErrorCodeEnum.ARGUMENT_VALID_ERROR.getCode(),"角色名或角色编码重复请修改!");
        }
    }

    @Override
    public List<SysRoleVO> allRole() {
        return roleDomainConvert.poListToVoList(this.list(new LambdaQueryWrapper<SysRolePO>().eq(SysRolePO::getStatus, StatusEnum.USABLE.getCode())));
    }
}
